package com.hou.educenter.controller;


import com.hou.commonutils.JwtUtils;
import com.hou.commonutils.R;
import com.hou.commonutils.ordervo.UcenterMemberOrder;
import com.hou.educenter.entity.UcenterMember;
import com.hou.educenter.entity.vo.RegistorVo;
import com.hou.educenter.service.UcenterMemberService;
import org.apache.http.HttpRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-11-19
 */
@RestController
@RequestMapping("/educenter/member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService memberService;

    //登录
    @PostMapping("login")
    public R loginUser(@RequestBody UcenterMember member) throws Exception {
        //member对象封装手机号和密码
        //调用service方法实现登录
        //返回token值，使用jwt生成
        String token = memberService.login(member);
        return R.ok().data("token",token);
    }

    // 注册
    @PostMapping("register")
    public R register(@RequestBody RegistorVo registorVo) throws Exception {
        Boolean register = memberService.register(registorVo);
        if (register) {
            return R.ok();
        }
        return R.error().message("注册失败");
    }

    // 根据token获取用户数据
    @GetMapping("getUserInfo")
    public R getUserInfo(HttpServletRequest request) {

        String memberId = JwtUtils.getMemberIdByJwtToken(request);

        // 调用数据库查询用户信息
        UcenterMember member = memberService.getById(memberId);

        return R.ok().data("userInfo",member);
    }

    // 根据用户id获取信息
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){

        UcenterMember member = memberService.getById(id);

        // 将member的值给UcentermemberOrder
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);

        return ucenterMemberOrder;

    }

    // 查询某一天注册人数
    @GetMapping("countRegisterDay/{day}")
    public R countRegisterDay(@PathVariable String day) {

        Integer member = memberService.countRegisterDay(day);

        System.out.println(member);

        return R.ok().data("countRegister",member);
    }

}

